package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @author Administrator
 * @date 2023/5/16 16:24
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 登录
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){

        System.out.println("请求成功");
        System.out.println(employee.getPassword());

        //把密码md5进行加密
        String password = employee.getPassword();
        password= DigestUtils.md5DigestAsHex(password.getBytes());

        //2.判断数据库是否有这个用户
        LambdaQueryWrapper<Employee> qw=new LambdaQueryWrapper<>();
        qw.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(qw);
        if(emp==null){
            return R.error("没有这个用户,登录失败");
        }

        //判断密码是否正确
        if(!emp.getPassword().equals(password)){
            return R.error("密码错误,登录失败");
        }

        //判断用户是否禁用
        if(emp.getStatus()==0){
            return R.error("用户禁用,登录失败");
        }

        //登录成功,把id放入Session,并且返回结果
        System.out.println("登录成功");
        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    /**
     * 员工退出
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功,再见");
    }

    /**
     * 添加员工
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
        log.info("新增员工信息 {}",employee);
        //设置默认密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //调用方法进行保存
        employeeService.save(employee);
        return R.success("添加成功");
    }

    /**
     * 员工分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page>page(int page,int pageSize,String name){
        log.info("{},{},{}",page,pageSize,name);
        //构造分页构造器
        Page pageInfo=new Page(page,pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Employee>lqw=new LambdaQueryWrapper<>();
        //设置构造条件
        lqw.like(StringUtils.isNotEmpty(name),Employee::getUpdateTime,name);
        //排序条件
        lqw.orderByDesc(Employee::getUpdateTime);
        //执行
        employeeService.page(pageInfo);
        return R.success(pageInfo);
    }

    /**
     * 根据id修改
     * @param employee
     * @return
     */
    @PutMapping
    public R<String>update(HttpServletRequest request,@RequestBody Employee employee){
        long emId = (long) request.getSession().getAttribute("employee");
        //更新数据
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    /**
     * 修改员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable long id){
        Employee employee = employeeService.getById(id);
        if(employee!=null){
            return R.success(employee);
        }
       return R.error("修改错误");
    }
}
